package com.pactera.madp.cp.controller.oms.taskpub;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.security.annotation.Inner;
import com.pactera.madp.cp.api.dto.taskpub.TaskPageDTO;
import com.pactera.madp.cp.api.dto.taskpub.TaskPubDto;
import com.pactera.madp.cp.api.dto.taskpub.TaskPubUpdateDto;
import com.pactera.madp.cp.api.vo.task.TaskDetailsByEmployeeVO;
import com.pactera.madp.cp.api.vo.task.TaskDetailsByIdVO;
import com.pactera.madp.cp.api.vo.task.TaskPageVO;
import com.pactera.madp.cp.service.TaskPubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;

import javax.annotation.Resource;

/**
 * 任务发布管理
 */
@RestController
@RequestMapping("/taskpub")
@Api(value = "taskpub", tags = "任务发布管理")
@AllArgsConstructor
public class TaskPubController {

    @Resource
    private TaskPubService taskPubService;

    // 分页查询
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public R<Page<TaskPageVO>> page(TaskPageDTO dto, Page page) {
        return R.ok(taskPubService.getPageByOut(dto,page));
    }

    // 详情
    @GetMapping("/{id}")
    @ApiOperation("获取任务详情")
    public R<TaskDetailsByIdVO> getById(@PathVariable("id") Integer id) {
        return R.ok(taskPubService.getTaskDetailsById(id));
    }

    @GetMapping("/revoke/{id}")
    @ApiOperation("撤销待审批任务")
    public R revoke(@PathVariable("id") Integer id) {
        return R.ok(taskPubService.revoke(id));
    }

    // 新建任务
    @PostMapping
    @ApiOperation(value = "新建任务")
    public R createTask(@Validated @RequestBody TaskPubDto dto) {
        return taskPubService.createTask(dto) ? R.ok() : R.failed();
    }

    // 编辑
    @PutMapping
    @ApiOperation(value = "编辑任务")
    public R editTask(@Validated @RequestBody TaskPubUpdateDto dto) {
        return taskPubService.updateTask(dto) ? R.ok() : R.failed();
    }

    // 发布
    @PutMapping("/update-status")
    @ApiOperation(value = "定时启动已发布任务")
    @Inner
    public void startTask() {
        RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(),true);
        taskPubService.startTask();
    }

    @GetMapping("/complete-employee")
    @ApiOperation("已完成成员名单")
    public R<Page<TaskDetailsByEmployeeVO>> getCompleteEmployee(@RequestParam("taskId") Integer taskId,Page page){
        return R.ok(taskPubService.getCompleteEmployee(taskId, page));
    }

    @GetMapping("/incomplete-employee")
    @ApiOperation("未完成成员名单")
    public R<Page<TaskDetailsByEmployeeVO>> getIncompleteEmployee(@RequestParam("taskId") Integer taskId, Page page){
        return R.ok(taskPubService.getIncompleteEmployee(taskId, page));
    }

    @PutMapping("/one-click-reminder")
    @ApiOperation("未完成成员提醒")
    public void oneClickReminder(@RequestParam("taskId") Integer taskId){
        taskPubService.oneClickReminder(taskId);
    }

    @PutMapping("/task-completion-statistics")
    @ApiOperation("完成任务情况统计")
    @Inner
    public void taskCompletionStatistics(){
        taskPubService.taskCompletionStatistics();
    }

    @PutMapping("/release-task")
    @ApiOperation("发布任务")
    public R releaseTaskById(@RequestParam("id") Integer id){
        return taskPubService.releaseTaskById(id) ? R.ok() : R.failed();
    }

    @DeleteMapping("/overdue-tasks")
    @ApiOperation("删除过期30天的任务")
    @Inner
    public void deleteOverdueTasks(){
        taskPubService.deleteOverdueTasks();
    }

}
